import React from 'react'
import { View, Image, } from 'react-native'
import { Lt, Dialog, Images, Label, Jpp, Jx, Color, Button } from "../_utils"
import { Net, NET_RI, NET_STR } from "../_net"
import { eApp } from './_home';
import { cTemp } from '../_util';
import { eRole } from '../me/_me';

// 商品详情页底部 收藏 分享赚 自购省
export class GoodsBottom extends React.PureComponent{
    constructor(props){
        super(props);
        this.state = {
            isFavorited : false,
        }
        this.onFavorite = this.onFavorite.bind(this);
    }
    // 收藏
    onFavorite(){
        if(this.state.isFavorited){
            Dialog.msg1("已收藏");
            return;
        }
        const { data, onImage } = this.props;
        const obj = {
            agent:data.agent,
            volume:data.volume,
            image: data.imgUrl || (onImage && onImage()) || "",
            promotion_rate: data.commissionRate,
            coupon:data.zk_money,
            src:eApp.idxs[data.appIdx],
            price:data.price,
            coupon_price:data.zk_price,
            goodId:data.goodId,
            title:data.goodName,
        };
        console.warn(Jx.stringify(obj));
        Net.request("collect/add", obj, {NET_STR, NET_RI}).then(r=>{
            Dialog.msg1("收藏成功");
            this.setState({isFavorited:true});
        });
    }
    render(){
        const { data, onShare, onBuy } = this.props;
        const btnHeight = 48;
        let agent = data.agent || "";
        let shareSelf = "分享";
        let buySelf = "购买";
        if(agent && cTemp.getRoleId() != eRole.FANS){
            shareSelf = "分享赚" + agent;
            buySelf = "自购省" + agent;
        }
        return <View style={{flexDirection:"row"}}>
            <Button.Pure1 onPress={this.onFavorite}style={{backgroundColor:"white", borderRadius:0, width:"20%", height:btnHeight, justifyContent:"center"}}>
                <View style={{flexDirection:"row", alignSelf:"center", justifyContent:"center", alignItems:"center"}}>
                    <Image source = {this.state.isFavorited ? Images.shoucang : Images.shoucang_sp}/>
                    <Label style={{fontSize:Lt.c16, color: Color.c9}}>收藏</Label>
                </View>
            </Button.Pure1>
            <Button.Pure1 onPress={onShare || null} title={shareSelf} style={{backgroundColor:Color.c3, borderRadius:0, width:"40%", height:btnHeight}}/>
            <Button.Pure1 onPress={onBuy || null}
                title={buySelf} 
                style={{borderRadius:0, width:"40%", height:btnHeight}}
            />
        </View>
    }
}